package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.pojo.order.Order;
import com.qingcheng.service.order.OrderService;
import com.qingcheng.service.order.WxPayService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/pay")
public class PayController {

    @Reference
    private OrderService orderService;

    @Reference
    private WxPayService wxPayService;


    @GetMapping("/createNative")
    public Map createNative(String orderId){

        String username = SecurityContextHolder.getContext().getAuthentication().getName();

        Order order = orderService.findById(orderId);
        if(order!=null){

            if("0".equals(order.getPayStatus())  && "0".equals(order.getOrderStatus()) &&  username.equals(order.getUsername()) ){
                Map map = wxPayService.createNative(orderId, order.getPayMoney(), "http://www.baidu.com/pay/notifyURL");
                return map;

            }else{
                return null;
            }

        }else{

            return null;
        }

    }

    @RequestMapping("notifyURL")
    public void notifyURL(){
        //1:解析微信支付给的回调结果  succ  fail   xml通讯

        //2:判断 succ  修改订单的状态  order status [待支付、支付中、支付成功、代发货。已发货]、 payStatus[ 待支付、支付中、支付失败]、


        //3:告诉微信你的回调我收到了  succ


        System.out.println("微信平台主动调用我们的接口，用来做微信支付回调通知 ");
    }

}
